package com.devin.controller;

import com.devin.pojo.Role;
import com.devin.pojo.UserInfo;
import com.devin.service.RoleService;
import com.devin.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @RequestMapping("/getUsers")
    @Secured("ROLE_ADMIN")
    public ModelAndView getUsers(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                 @RequestParam(value = "size",defaultValue = "5") Integer size){
        List<UserInfo> users = userService.getUsers(page,size);
        PageInfo<UserInfo> userInfoPageInfo = new PageInfo<UserInfo>(users);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("userPageInfo",userInfoPageInfo);
        modelAndView.setViewName("users-pages-list");
        return  modelAndView;
    }

    @RequestMapping("/delUser")
    public String delUser(Integer userId){
        userService.delUser(userId);
        return "redirect:/users/getUsers";
    }

    @RequestMapping("/getRoles2UserAdded")
    public ModelAndView getRoles2UserAdded(){
        List<Role> roles = roleService.getRoles();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("roles",roles);
        modelAndView.setViewName("user-add");
        return modelAndView;
    }

    @RequestMapping("/addUser")
    public String addUser(UserInfo user,Integer[] roleIds){
        userService.addUser(user,roleIds);
        return "redirect:/users/getUsers";
    }

    @RequestMapping("/getUsersDetail")
    public ModelAndView getUsersDetail(Integer userId){
        UserInfo users = userService.getUsersDetail(userId);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("users",users);
        modelAndView.setViewName("users-detail");
        return modelAndView;
    }

    @RequestMapping("/getRoles2Json")
    @ResponseBody
    public List<Role> getRoles2Json(Integer userId){
        List<Role> roles =roleService.getRoles2Json(userId);
        return roles;
    }

    @RequestMapping("/addRoles")
    public String addRoles(Integer userId,Integer[] roleIds){
        userService.addRoles(userId,roleIds);
        return "redirect:/users/getUsersDetail?userId="+userId;
    }

    @RequestMapping("/delRoleByUserId")
    public String delRoleByUserId(Integer userId,Integer roleId){
        userService.delRoleByUserId(userId,roleId);
        return "redirect:/users/getUsersDetail?userId="+userId;
    }
}
